package com.ysalliance.getFan.organization.ui.h5;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.json.JSONObject;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

import static com.ysalliance.getFan.organization.FanerConstant.ENTER;


/**
 * 一般的网页
 *
 * @author lucas
 * created at 2020-01-23 21:19
 */
public class CommonH5Activity extends LucasBaseActivity {


    @BindView(R.id.web_container)
    LinearLayout web_container;
    @BindView(R.id.tv_title)
    TextView tv_title;


    protected AgentWeb mAgentWeb;
    //请求的网址
    private String askUrl = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int bindContentView() {
        return R.layout.activity_common_h5;
    }

    @Override
    public void afterView() {
        super.afterView();

        askUrl = getIntent().getStringExtra("url");
        LogUtils.e("h5页面收到的url:", askUrl);

        //展示缓冲条
        mProgressBarHandler.show(); // To show the progress bar
        initData();




    }


    public void initData() {
        //同步cookies
//        AgentWebConfig.syncCookie("https://api.getfan.cn", LucasUtil.pref_get(mContext,"cookie"));
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(web_container, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                .closeIndicator()    //取消进度条     默认是.useDefaultIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时，弹窗咨询用户是否前往其他应用
                .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                .createAgentWeb()
                .ready()
                .go(askUrl);



        if(mAgentWeb!=null){
            //注入对象
            mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb,mContext));

        }

    }


    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
            Log.e("Info", "BaseWebActivity onPageStarted");
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            CookieManager cookieManager = CookieManager.getInstance();
            String CookieStr = cookieManager.getCookie(url);
            LogUtils.d("Cookies:", CookieStr);

            super.onPageFinished(view, url);
        }


    };


    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            //do you work
            Log.i("Info", "onProgress:" + newProgress);

            if (newProgress == 100) {
                mProgressBarHandler.hide();
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            //设置标题
            if (tv_title != null) {
                tv_title.setText(title);
            }
        }
    };


    @Override
    public void onBackPressed() {
        if(askUrl.equals(ENTER)){
            super.onBackPressed();
        }else {
            if (!mAgentWeb.back()) {
                super.onBackPressed();
            }
        }

    }


    @Override
    public void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }


    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }




}
